package com.goods.controller.business;

import com.goods.business.service.ProductCategoryService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "业务模块-物资分类相关接口")
@RestController
@RequestMapping("/business/productCategory")
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;

    @ApiOperation(value = "查询物资类别列表及分类树")
    @GetMapping("/categoryTree")
    public ResponseBean<PageVO<ProductCategoryTreeNodeVO>> categoryTree(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize

    ){
        PageVO<ProductCategoryTreeNodeVO> treeNodeVOPageVO =
                productCategoryService.categoryTree(pageNum,pageSize);
        return ResponseBean.success(treeNodeVOPageVO);
    }

    @ApiOperation(value = "查询父级分类")
    @GetMapping("/getParentCategoryTree")
    public ResponseBean<List<ProductCategoryTreeNodeVO>> getParentCategoryTree(){
        List<ProductCategoryTreeNodeVO> treeNodeVOS = productCategoryService.getParentCategoryTree();
        return ResponseBean.success(treeNodeVOS);
    }

    @ApiOperation(value = "添加分类")
    @PostMapping("/add")
    public ResponseBean add(@RequestBody ProductCategory productCategory){
        productCategoryService.add(productCategory);
        return ResponseBean.success();
    }

    @ApiOperation(value = "修改分类,数据回显")
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id){
        ProductCategory productCategory = productCategoryService.edit(id);
        return ResponseBean.success(productCategory);
    }

    @ApiOperation(value = "修改分类")
    @PutMapping("/update/{id}")
    public ResponseBean update(@RequestBody ProductCategory productCategory,
                               @PathVariable Long id){
        productCategoryService.update(productCategory);
        return ResponseBean.success();
    }

    @ApiOperation(value = "删除分类")
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id){
        productCategoryService.delete(id);
        return ResponseBean.success();
    }


}
